<template>
	<view class="create">
		<view class="formsBox">
			<uni-forms ref="form" :modelValue="formData" :label-width="100" :rules="rules">
				<uni-forms-item label="申请人" name="applicant">
					<uni-easyinput type="text" v-model="formData.applicant" placeholder="请输入申请人" />
				</uni-forms-item>
				<uni-forms-item label="发文科室" name="deptId">
					<deptSelect v-model="formData.deptId" :disabled="!!processId" />
				</uni-forms-item>
				<uni-forms-item label="文件标题" name="title">
					<uni-easyinput type="text" :disabled="!!processId" v-model="formData.title" placeholder="请输入文件标题" />
				</uni-forms-item>
				<uni-forms-item label="发文类型" name="type">
					<uni-data-select
						:disabled="!!processId"
					    v-model="formData.type"
					    :localdata="oaDocumentType"
						placeholder="请选择发文类型"
					></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="年份" name="year" >
					<picker mode="date" fields="year" :disabled="!!processId" :value="formData.year" class="pickerClass" @change="bindPickerChange">
						<view class="uni-input" style="height: 100%;">{{formData.year}}</view>
					</picker>
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="buttonBox" v-if="!processId">
			<button class="button" @click="submit" type="primary">提交</button>
		</view>
	</view>
</template>

<script>
	import {createOaApplicationNumber} from '@/api/oa/applicationNumber/index.js'
	import deptSelect from '@/components/user/deptSelect.vue'
	import {getIntDictOptions} from '@/utils/auth.js'
	export default {
		props:{
			processId:'',
			propFromData:{},
			// remainingNum:0,
		},
		components:{
			deptSelect
		},
		watch:{
			propFromData(val){
				this.formData = val;
				this.formData.type = String(this.formData.type);
				let deptId = {
					name:val.deptName,
					id:val.deptId
				};
				this.formData.deptId = deptId
			},
		},
		data() {
			return {
				formData:{
					applicant:this.$store.state.user.name,
					type:'',
					title:'',
					deptId:'',
					year:'2025'
				},
				oaDocumentType: [],
				// remaining:0,
				rules: {
					applicant: {
						rules: [{
							required: true,
							errorMessage: '请输入请假申请人',
						}]
					},
					title: {
						rules: [{
							required: true,
							errorMessage: '请输入文件标题',
						}]
					},
					type: {
						rules: [{
							required: true,
							errorMessage: '请选择发文类型',
						}]
					},
					deptId:{
						rules: [{
							required: true,
							errorMessage: '请选择发文科室',
						}]
					},
				},
				show:false
			};
		},
		methods:{
			// 点击提交
			submit(){
				uni.showLoading()
				this.$refs.form.validate().then(async res => {
					try{
						let {deptId,...objs} = this.formData
						objs.deptId = deptId.id
						objs.deptName = deptId.name
						console.log(objs)
						let info = await createOaApplicationNumber(objs);
						console.log(info)
						uni.hideLoading()
						uni.showToast({
							title:'发起成功',
							icon:'none',
							success: () => {
								setTimeout(()=>{
									uni.redirectTo({
										url:'/pages/process/applicationNumber/index'
									})
								},1500)
							}
						})
					}catch(err){
						console.log(err)
						uni.hideLoading()
					}
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
			bindPickerChange(val){
				console.log(val)
				this.formData.year = val.detail.value;
			}
		},
		
		async onLoad() {
			this.oaDocumentType = await getIntDictOptions('oa_document_type');
			
			// const currentDate = new Date();
			// const currentYear = currentDate.getFullYear();
			// this.formData.year = currentYear
			// let info = await getDocumentNumber({
			// 	year:this.formData.year,
			// 	type:this.formData.type
			// })
			// this.formData.mark = info.data
		},
		async mounted() {
			this.oaDocumentType = await getIntDictOptions('oa_document_type');
		}
	}
</script>

<style lang="less" scoped>
.create{
	width: 100%;
	height: auto;
	padding-top: 20rpx;
	.formsBox{
		width: 100%;
		height: auto;
		background-color: #fff;
		padding: 20rpx;
		box-sizing: border-box;
		.uni-forms-item{
			margin-bottom: 30rpx;
		}
	}
	.buttonBox{
		width: 100%;
		height: auto;
		padding: 0 20rpx;
		box-sizing: border-box;
		margin-top: 40rpx;
	}
	.pickerClass{
		height: 100%;display: flex;align-items: center;border: 1rpx solid #e5e5e5;border-radius: 10rpx;padding-left: 20rpx;
	}
}
</style>
